#ifndef EPOLL_H_INCLUDED
#define EPOLL_H_INCLUDED

#if defined(_WIN32)
#include "wepoll.h"
#define EPOLLET EPOLLIN
#else
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <strings.h>
#include <vector>
#endif

#include "internal.h"

class Epoll
{
public:
	Epoll(int flags = EPOLL_CLOEXEC, int noFile = 102400);
	~Epoll();

	int addfd(int fd, uint32_t events = EPOLLIN, bool ETorNot = false);
	void modfd(int fd, uint32_t events = EPOLLIN, bool ETorNot = false);
	void delfd(int fd);
	int wait(int timeout = 200);
	int getEventOccurfd(int eventIndex) const;
	uint32_t getEvents(int eventIndex) const;

public:
	bool isValid()
	{
#if defined(_WIN32)
		if (m_epollfd == nullptr)
#else
		if (m_epollfd == -1)
#endif
			return false;
		return true;
	}
	void close()
	{
		if (isValid())
		{
#if defined(_WIN32)
			epoll_close(m_epollfd);
			m_epollfd = nullptr;
#else
			::close(m_epollfd);
			m_epollfd = -1;
#endif
		}
	}

private:
	std::vector<struct epoll_event> events;
#if defined(_WIN32)
	HANDLE m_epollfd;
#else
	int m_epollfd;
#endif
	int fdNumber;
	int nReady;
private:
	struct epoll_event event;
};

#endif // EPOLL_H_INCLUDED
